package com.yh.action;

import com.alibaba.fastjson.JSONArray;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.yh.model.HouseModel;
import com.yh.model.HouseTypeModel;
import com.yh.model.StreetModel;
import com.yh.model.TownModel;
import com.yh.service.HouseService;
import com.yh.service.SelectService;
import com.yh.template.ResultTemplate;
import com.yh.util.ByteStream;
import com.yh.util.Page;
import com.yh.util.RandomUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@Setter
@Getter
public class HouseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {

    private int id;
    private String title;
    private String price;
    private String area;
    private String street;
    private String type;
    private HouseModel houseEntity;
    private Object obj;
    private List<HouseModel> houseModelList;

    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext context;


    public String add(){
        String result = "";
        int count = -1;
        String path = "";
        try {
            houseEntity.setHid((Integer) request.getSession().getAttribute("userid"));
             count = new HouseService().addHouse(houseEntity);
             if (count > 0){
                 obj = JSONArray.toJSON(new ResultTemplate("200","添加成功",count));
             }else{
                 obj = JSONArray.toJSON(new ResultTemplate("202","添加失败",count));
             }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result = Action.SUCCESS;
        return result;
    }

    public String update(){
        String result = "";
        int count = -1;
        try {
            count = new HouseService().updateHouse(houseEntity);
            if (count > 0){
                obj = JSONArray.toJSON(new ResultTemplate("200","修改成功",count));
            }else{
                obj = JSONArray.toJSON(new ResultTemplate("202","修改失败",count));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String del(){
        String result = "";
        int count = -1;
        try {
            count = new HouseService().delHouse(id);
            if (count > 0){
                obj = JSONArray.toJSON(new ResultTemplate("200","删除成功",count));
            }else{
                obj = JSONArray.toJSON(new ResultTemplate("202","删除失败",count));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result = Action.SUCCESS;
        return result;
    }

    private int startPage;
    public String find(){
        System.out.println(startPage);
        String result  = "";
        try {
            Page<HouseModel> page = new HouseService().find(startPage,4);
            obj = JSONArray.toJSON(page);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String findone(){
        String result = "";
        try {
            HouseModel house = new HouseService().findoneHouse(id);
            obj = JSONArray.toJSON(house);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String like(){
        String result = "";
        try {
            System.out.println(area);
//            System.out.println(title+"============="+price);
            List<HouseModel> list = new HouseService().likeHouse(title,type,area,price,street);
            obj = JSONArray.toJSON(list);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String type(){
        String result = "";
        try {
            List<HouseTypeModel> list = new SelectService().alltype();
            obj = JSONArray.toJSON(list);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String town(){
        String result = "";
        try {
            List<TownModel> list = new SelectService().alltown();
            obj = JSONArray.toJSON(list);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String street(){
        String result = "";
        try {
            List<StreetModel> list = new SelectService().allstreet();
            obj = JSONArray.toJSON(list);
            result = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String user(){
        String resutl = "";
        try {
            int userid = (Integer) request.getSession().getAttribute("userid");
            Page<HouseModel> page = new HouseService().userHouse(userid,startPage,4);
            obj = JSONArray.toJSON(page);
            resutl = Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resutl;
    }



    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
    }
}
